package dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import pojo.Categories;
import pojo.Food;
import pojo.Merchant;
import dao.FoodDao;
@Repository(value="foodDao")
public class FoodDaoImpl implements FoodDao {
	@PersistenceContext(name="un")
	private EntityManager manager;
	
	@Override
	public List<Food> findAll(String m_id) {
		List<Food> foodList = manager.createQuery("select f from Food f where f.merchant.id =:m_id")
				.setParameter("m_id", m_id)
				.getResultList();
		return foodList;
	}

	@Override
	public List<Food> findByType(String typeId, String m_id) {
		List<Food> foodList = manager.createQuery("from Food f where f.categories.id=:t_id and f.merchant.id =:m_id")
		.setParameter("t_id", typeId)
		.setParameter("m_id", m_id)
		.getResultList();
		
		return foodList;
	}

	@Override
	public void addFood(Food food, String m_id, String c_id) {
    	Categories categories = manager.find(Categories.class, c_id);
    	Merchant merchant = manager.find(Merchant.class, m_id);
    	manager.persist(food);
    	
    	categories.getFoodList().add(food);
    	merchant.getFoodList().add(food);
    	
    	food.setCategories(categories);
    	food.setMerchant(merchant);
    	
        manager.persist(food);
        manager.persist(categories);
        manager.persist(merchant);
	}

}
